/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uit.tkorg.coauthorgraph.servlet.graph;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.dom4j.Document;
import uit.tkorg.coauthorgraph.bo.CoAuthorGraphBO;
import uit.tkorg.coauthorgraph.bo.ConferenceBO;
import uit.tkorg.coauthorgraph.utility.CommonXMLUtility;

/**
 *
 * @author Tin Huynh
 */
public class CalculateMetricAndExportToFileServlet extends HttpServlet {
   
   protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        File fileUpLoad = null;
        File destinationDir = null;
        try {
            // Check that we have a file upload request
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            List /* FileItem */ items = upload.parseRequest(request);
            Iterator iter = items.iterator();
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                if (!item.isFormField()) {
                    String fieldName = item.getFieldName();
                    String fileName = item.getName();
                    String contentType = item.getContentType();
                    boolean isInMemory = item.isInMemory();
                    long sizeInBytes = item.getSize();
                    byte[] content = item.get();
                    String realPath = getServletContext().getRealPath("");
                    destinationDir = new File(realPath);
                    fileUpLoad = new File(destinationDir, fileName);
                    item.write(fileUpLoad);
                }
            }

            Document doc = CommonXMLUtility.readXMLFile(fileUpLoad);
            ConferenceBO confBO = new ConferenceBO();
            ArrayList inputConfList = confBO.getInputConfFromDOMObject(doc);
            CoAuthorGraphBO coAuthorGraphBO = new CoAuthorGraphBO();
            coAuthorGraphBO.calculateMetricAndExportToFile(inputConfList, "C:\\output_conf_classification.txt", "C:\\output_PC_conf.txt");

            request.setAttribute("resultMessage",
                    "Take a look at Output Text file in ...\\output_conf_classification.txt "
                    + "& ...\\output_PC_conf.txt to see the result");
            RequestDispatcher dispatcher = request.getRequestDispatcher("./jsp/CalculateMetricAndExportToTextFile.jsp");
            dispatcher.forward(request, response);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
